<?php
namespace Home\Controller;


class EmailController extends HomeController {
    private $_model = null; //数据库操作类
    //初始化操作
        public function _initialize(){
            parent::_initialize();
            $this->_model = D('Email');
        }

    //邮箱遍历
    public function index(){

        $un = $_SESSION['user']['name'];
       // dump($un);exit;
       // ->buildSql();
       $data = M('Email')->where(array('receiveid'=>array('eq',$un)))->order('id desc')->select();
          
       $this->assign('title','邮件列表');
       $this->assign('list',$data);
       $this->display('Email/index');
    }

    //发送过的邮件
    public function ewrite(){

        $an = $_SESSION['user']['name'];
       // dump($un);exit;
       $data = M('Email')->where(array('sendid'=>array('eq',$an)))->order('id desc')->select();
        
       $this->assign('title','发件箱');
       $this->assign('list',$data);
       $this->display();
    }

    //邮件管理的详情页面
    public function econtent(){
        $id = I('get.id');
        $result = M('Email')->where(array('id'=>array('eq',$id)))->save(array('estate'=>'2'));
        // var_dump($result);exit;
        $list = M('Email')->where(array('id'=>array('eq',$id)))->select();

        //dump($list);exit;
        $this->assign('econtent',$econtent['econtent']);
        $this->assign('list',$list);
        $this->display(); 
    }

    //发送过的邮件详情页面
    public function content(){
        $id = I('get.id');
        
        // var_dump($result);exit;
        $list = M('Email')->where(array('id'=>array('eq',$id)))->select();

        //dump($list);exit;
        // $this->assign('econtent',$econtent['econtent']);
        $this->assign('list',$list);
        $this->display(); 
    }

    //增加
    public function add(){
        //查部门
        $arr1 = M('group')->select();
        $grop = [];
     
        foreach ($arr1 as $k => $v) {
            $group[$v['id']] = $v['groupname'];
        }

        $this->assign('group',$group);
        $this->assign('title','新邮件');
        $this->display('Email/add');
            
    }

   
    public function doAjax()
    {
        if (!IS_AJAX) {
            $this->error('你是不是迷路了.....');
            exit;
        }
        //接受ajax传值
        $val = I('post.groupid');
        // dump($val);exit;
        $data = M('user')->field('name,id')->where(array('groupid'=>$val))->select();
       
         $this->ajaxReturn($data);
    }

    public function insert(){
        if (empty($_POST)) {
            $this->error('请填写数据!', U('Email/add'));
            exit;
        }

        $rec = $_POST['receiveid'];
        $rec  = rtrim($rec,',');
        $info = explode(',',$rec);
        $num = 0;
        foreach ($info as  $k => $v) {
           $data['receiveid'] = $v;
           $data['sendid'] = $_POST['sendid'];
           $data['etitle'] = $_POST['etitle'];
           $data['econtent'] = $_POST['econtent'];

            if(!$this->_model->create($data)){
                    $this->error($this->_model->getError());
                    exit;
                }
            if($this->_model->add() > 0){
                $num +=1;
            }
        }
            //添加操作,返回自增ID
            if($num = count($info)  ){
                    $this->success("邮件发送成功！",U('Email/index'));
                }else{
                    $this->error("邮件发送失败！");
                }
    }

        

    //删除
    public function del()
    {
        
        //接收参数
        $id = I('get.id/d');
        // echo $id;exit;
        if (M('Email')->delete($id) > 0) {
            $this->success('恭喜您,邮件删除成功!', U('index'));
        } else {
            $this->error('邮件删除失败!');
        }
    }


}